import { appTokenInstance } from "../../../utils/appToken"
import { defaultConfig, shareAppMessageSet } from "../../../utils/config"
import { dataCollection } from "../../../utils/dataCollection"
import { loginInstance } from "../../../utils/login"
import { phoneHide } from "../../../utils/util"

var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    service: [
      { icon: '../../../images/wode_icon_wdbm@2x.png', loginStatus: true, text: '我的报名', type: 'navigate', url: '../../../pages/personalCenter/sign-up/sign-up', describe: '' },
      { icon: '../../../images/wode_icon_wddd@2x.png', loginStatus: true, text: '我的订单', type: 'navigate', url: '../../../pages/order/list/list', describe: '' },
      { icon: '../../../images/wode_icon_lxkf@2x.png', loginStatus: false, text: '联系客服', type: 'tel', url: '', describe: '' },
      { icon: '../../../images/wode_icon_yhxy@2x.png', loginStatus: false, text: '用户协议', type: 'navigate', url: '../../../pages/personalCenter/protocol/protocol?type=USER_AGREEMENT', describe: '' },
      { icon: '../../../images/wode_icon_ysxy@2x.png', loginStatus: false, text: '隐私协议', type: 'navigate', url: '../../../pages/personalCenter/protocol/protocol?type=PRIVACY_AGREEMENT', describe: '' },
      // { icon: '../../../images/wode_icon_ysxy@2x.png', loginStatus: false, text: '优惠券', type: 'webUrl', url: 'https://drive-h5.chexinlian.com/coupon', describe: '' },
    ],
    loginStatus: false,
    userName: '',
    userPhone: '',
    userInfo: <any>{
      openId: ''
    },
    isPC: false,
    themeCode: '',
    backgroundImage: '',
    headImage: ''
  },

  onLoad() {
    //从本地获取当前选择的城市编码
    const themeCode = wx.getStorageSync('themeCode')
    this.setData({
      themeCode,
      backgroundImage: app.globalData.dictMap.get('personalCenterBackgroundImage'),
      headImage: app.globalData.dictMap.get('defaultHeadImage'),
      'service[2].describe': wx.getStorageSync('currentDrivingSchool').customerServiceTel ?? defaultConfig.customerServiceTel
    })

    this.getUserInfo()

    wx.getSystemInfo({
      success: (res: any) => {
        if (res.platform === "windows" || res.platform === "mac") {
          //电脑端 
          this.setData({
            isPC: true
          })
        }
      }
    })
  },
  onHide() {
    dataCollection.setPageMapData('leave')
  },
  onUnload() {
    dataCollection.setPageMapData('leave')
  },
  onShow() {
    dataCollection.setPageMapData('enter', '个人主页')
    this.getUserInfo()
  },
  getUserInfo() {
    const userInfo = wx.getStorageSync('userInfo') || ''
    const loginStatus = wx.getStorageSync('loginStatus')
    //没有用户信息，但是登录了
    if (!userInfo && loginStatus) {
      this.setData({
        loginStatus: loginStatus
      })
    } else if (loginStatus) {
      this.setData({
        userInfo: userInfo,
        userPhone: phoneHide(userInfo.phone),
        loginStatus: loginStatus
      })
    } else {
      this.setData({
        userInfo: null,
        userPhone: '',
        loginStatus: false
      })
    }
  },
  //打开对应的页面
  onOpenPage(res: any) {
    const obj = this.data.service[res.currentTarget.dataset.index]
    //先判断点击打开的页面是否需要登录
    if (obj.loginStatus && !this.data.loginStatus) {
      wx.showModal({
        title: '请先登录再操作',
        success: (e: any) => {
          if (e.confirm) {
            this.onLogin()
          } else if (e.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      return
    }
    if (obj.type === 'navigate') {
      wx.navigateTo({ url: obj.url })
    } else if (obj.type === 'tel') {
      wx.makePhoneCall({ phoneNumber: obj.describe })
    } else if (obj.type === 'webUrl') {
      wx.navigateTo({ url: '/pages/web-view/web-view?url=' + obj.url })
    }
  },
  //跳转登录页
  onLogin() {
    if (!this.data.loginStatus) {
      wx.navigateTo({
        url: '/pages/login/login'
      })
    } else {
      //
    }
  },
  //退出登录
  onLogout() {
    console.log('退出')
    wx.showModal({
      title: '是否确认退出？',
      success: (e: any) => {
        if (e.confirm) {
          loginInstance.removeStorage()
          loginInstance.JudgmentExpirationInstance.clear()
          appTokenInstance.removeStorage()
          appTokenInstance.JudgmentExpirationInstance.clear()
          this.setData({
            loginStatus: false
          })
        } else if (e.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return { ...shareAppMessageSet() }
  }

})